package com.jianbing.util.gjj;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jianbing.entity.LoginStep;
import com.jianbing.entity.LoginStepDetail;
import com.jianbing.exception.ServiceException;
import com.jianbing.util.ColletionUtils;
import com.jianbing.util.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hantixiang on 12/1/2016.
 */
public class GjjParamUtils {
    private static final Log log = LogFactory.getLog(GjjParamUtils.class);
    public static final String NAME = "name", NOTE = "note", TYPE = "type";

    public static List<LoginStep> getLoginStepList(String jsonString) throws ServiceException {
        if (StringUtils.isEmpty(jsonString)) {
            throw new ServiceException("loginTypeId must input");
        }
        return JSON.parseArray(jsonString, LoginStep.class);
    }

    public static List<LoginStepDetail> getAllFirstLoginStepDetailList(List<LoginStep> loginSteps) throws ServiceException {
        if (ColletionUtils.isEmpty(loginSteps))
            return null;
        List<LoginStepDetail> list = new ArrayList<>();
        for (LoginStep loginStep : loginSteps) {
            LoginStepDetail loginStepDetail = getFirstLoginStepDetail(loginStep);
            loginStepDetail.setLoginTypeId(loginStep.getLoginTypeId());
            if (loginStepDetail != null) {
                list.add(loginStepDetail);
            }
        }
        return list;
    }

    public static LoginStepDetail getFirstLoginStepDetail(List<LoginStep> loginSteps, JSONObject paramJsonObjec) throws ServiceException {
        if (ColletionUtils.isEmpty(loginSteps))
            return null;
        for (LoginStep loginStep : loginSteps) {
            LoginStepDetail loginStepDetail = getFirstLoginStepDetail(loginStep);
            JSONArray container = JSON.parseArray(loginStepDetail.getLoginParam());
            boolean flag = checkInputLoginParamValid(container, paramJsonObjec);
            if (flag) {
                loginStepDetail.setLoginTypeId(loginStep.getLoginTypeId());
                return loginStepDetail;
            }
        }
        return null;
    }

    public static boolean checkInputLoginParamValid(LoginStepDetail loginStepDetail, JSONObject jsonObject) {
        if (loginStepDetail == null || StringUtils.isEmpty(loginStepDetail.getLoginParam()))
            return false;
        JSONArray container = JSON.parseArray(loginStepDetail.getLoginParam());
        return checkInputLoginParamValid(container, jsonObject);
    }

    public static boolean checkInputLoginParamValid(JSONArray container, JSONObject dataJsonObject) {
        log.info(dataJsonObject);
        log.info(container);
        boolean checkValid = false;
        if (container == null || dataJsonObject == null) {
            return checkValid;
        }
        int index = 0;
        String name = "";

        if (dataJsonObject.size() == 1) {
            for (String key :
                    dataJsonObject.keySet()) {
                if (checkNameIsJson(key) ) {
                    return true;
                }
            }
        }

        for (int i = 0; i < container.size(); i++) {
            JSONObject object = (JSONObject) container.get(i);
            name = object.get(NAME).toString().trim().replaceAll(" ", "");
            log.info(name);
            if (dataJsonObject.containsKey(name)) {
                index++;
            }
        }
        if (index == container.size()) {
            checkValid = true;
        }
        return checkValid;
    }

    private static boolean checkNameIsJson(String name) {
        if (StringUtils.isEmpty(name)) {
            return false;
        }
        try {
            JSON.parse(name);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static LoginStepDetail getFirstLoginStepDetail(LoginStep loginStep) throws ServiceException {
        List<LoginStepDetail> loginStepDetailList = loginStep.getLoginStepDetail();
        if (ColletionUtils.isEmpty(loginStepDetailList))
            throw new ServiceException("步骤为空");

        for (LoginStepDetail loginStepDetail : loginStepDetailList) {
            if (loginStepDetail.getPreStep() == null) {
                loginStepDetail.setLoginTypeId(loginStep.getLoginTypeId());
                return loginStepDetail;
            }
        }
        return null;
    }

    public static LoginStep getLoginStep(List<LoginStep> loginStepList, String loginStepId) {
        for (LoginStep loginStep : loginStepList) {
            if (loginStep.getLoginTypeId().equals(loginStepId)) {
                return loginStep;
            }
        }
        return null;
    }

    public static LoginStepDetail getNextLoginStepDetail(List<LoginStep> loginStepList, LoginStepDetail loginStepDetail) throws ServiceException {
        if (loginStepDetail.getNextStep() == null) {
            return null;
        }

        LoginStep loginStep = getLoginStep(loginStepList, loginStepDetail.getLoginTypeId());
        List<LoginStepDetail> loginStepDetailList = loginStep.getLoginStepDetail();
        if (ColletionUtils.isEmpty(loginStepDetailList))
            throw new ServiceException("步骤为空");

        for (LoginStepDetail nextStepDetail : loginStepDetailList) {
            if (nextStepDetail.getCurrentStep() == loginStepDetail.getNextStep()) {
                nextStepDetail.setLoginTypeId(loginStep.getLoginTypeId());
                return nextStepDetail;
            }
        }
        return null;
    }

    public static LoginStepDetail getNextLoginStepDetail(LoginStep loginStep, LoginStepDetail loginStepDetail) throws ServiceException {
        if (loginStepDetail.getPreStep() == null) {
            return null;
        }
        List<LoginStepDetail> loginStepDetailList = loginStep.getLoginStepDetail();
        if (ColletionUtils.isEmpty(loginStepDetailList))
            throw new ServiceException("步骤为空");

        for (LoginStepDetail nextStepDetail : loginStepDetailList) {
            if (nextStepDetail.getCurrentStep() == loginStepDetail.getNextStep()) {
                nextStepDetail.setLoginTypeId(loginStep.getLoginTypeId());
                return nextStepDetail;
            }
        }

        return null;
    }

    public static LoginStepDetail getCurrentLoginStepDetail(List<LoginStep> loginStepList, String loginTypeId, Integer currentStep) throws ServiceException {
        if (loginTypeId == null || currentStep == null || ColletionUtils.isEmpty(loginStepList)) return null;
        LoginStep loginStep = getLoginStep(loginStepList, loginTypeId);
        List<LoginStepDetail> loginStepDetailList = loginStep.getLoginStepDetail();
        if (ColletionUtils.isEmpty(loginStepDetailList))
            return null;

        for (LoginStepDetail loginStepDetail : loginStepDetailList) {
            if (loginStepDetail.getCurrentStep() == currentStep) {
                loginStepDetail.setLoginTypeId(loginTypeId);
                return loginStepDetail;
            }
        }
        return null;
    }

    public static LoginStepDetail getPreLoginStepDetail(LoginStep loginStep, LoginStepDetail loginStepDetail) throws ServiceException {
        if (loginStepDetail.getNextStep() == null) {
            return null;
        }
        List<LoginStepDetail> loginStepDetailList = loginStep.getLoginStepDetail();
        if (ColletionUtils.isEmpty(loginStepDetailList))
            throw new ServiceException("步骤为空");

        for (LoginStepDetail preLoginStepDetail : loginStepDetailList) {
            if (preLoginStepDetail.getCurrentStep() == loginStepDetail.getPreStep()) {
                preLoginStepDetail.setLoginTypeId(loginStep.getLoginTypeId());
                return preLoginStepDetail;
            }
        }
        return null;
    }


    public static void checkLoginParamFormat(String jsonString) throws ServiceException {
        try {
            List<LoginStep> loginSteps = getLoginStepList(jsonString);

            for (LoginStep loginStep : loginSteps) {
                LoginStepDetail loginStepDetail = getFirstLoginStepDetail(loginStep);
                checkCurrentStepParamValid(loginStepDetail.getLoginParam());
                while (loginStepDetail.getNextStep() != null) {
                    loginStepDetail = getNextLoginStepDetail(loginStep, loginStepDetail);
                    checkCurrentStepParamValid(loginStepDetail.getLoginParam());
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }


    private static void checkCurrentStepParamValid(String jsonStr) throws ServiceException {
        JSONArray array = JSON.parseArray(jsonStr);
        for (int i = 0; i < array.size(); i++) {
            JSONObject param = (JSONObject) array.get(i);
            if (!(param.containsKey(NOTE) && param.containsKey(NAME) && param.containsKey(TYPE))) {
                throw new ServiceException("登陆配置项请确认至少包含name,note,type");
            }
        }
    }

    public static void main(String[] args) throws ServiceException {

        String jsonStr = "[{\"111\":\"login1\",\"name\":\"登录方式1\",\"loginStepDetail\":[{\"currentStep\":1,\"stepName\":\"input_account\",\"loginParam\":[{\"name\":\"id\",\"note\":\"请输入账号\"}],\"preStep\":null,\"nextStep\":2},{\"currentStep\":2,\"stepName\":\"input_phone_code\",\"loginParam\":[{\"name\":\"phone\",\"note\":\"请输入手机号码\"},{\"name\":\"code\",\"note\":\"请输入验证码\"}],\"preStep\":1,\"nextStep\":3},{\"currentStep\":3,\"stepName\":\"input_phone_code2\",\"loginParam\":[{\"name\":\"phone\",\"note\":\"请输入手机号码2\"},{\"name\":\"code\",\"note\":\"请输入验证码2\"}],\"preStep\":2,\"nextStep\":null}]},{\"loginTypeId\":\"login2\",\"name\":\"登录方式2\",\"loginStepDetail\":[{\"currentStep\":1,\"stepName\":\"input_account\",\"loginParam\":[{\"name\":\"id\",\"note\":\"请输入账号\"}],\"preStep\":null,\"nextStep\":2},{\"currentStep\":2,\"stepName\":\"input_phone_code\",\"loginParam\":[{\"name\":\"phone\",\"note\":\"请输入手机号码\"},{\"name\":\"code\",\"note\":\"请输入验证码\"}],\"preStep\":1,\"nextStep\":3},{\"currentStep\":3,\"stepName\":\"input_phone_code2\",\"loginParam\":[{\"name\":\"phone\",\"note\":\"请输入手机号码2\"},{\"name\":\"code\",\"note\":\"请输入验证码2\"}],\"preStep\":2,\"nextStep\":null}]}]";

        //      System.out.println(GjjParamUtils.getFirstStep(jsonStr, "login1"));


        List<LoginStep> step = JSON.parseArray(jsonStr, LoginStep.class);

        System.out.println(step);
    }


}
